var scrollTopArr = new Array();
var pageSlideHold = 400;
var isNeedBackPage = true;
var last_pageIdOrUrl;
var last_param;
var last_effect;
var stFrameLevel = 1;
var last_speedTime;

window.onload = function(){
	$("iframe.lazy-frame").each(function(){
		var $iframe = $(this);
		var lazySrc = $iframe.attr("lazy-src");
	    if(lazySrc && lazySrc != ""){
	    	$iframe[0].onload = function(){
	    		this.style.opacity = "1";
	    	};
	    	$iframe[0].src = lazySrc;
	    	$iframe.removeAttr("lazy-src");
	    }
	});
};
function toShowBrowser(obj,objid){
	$("#browserType").removeClass("controlHide");
	$("#browserType").attr("currentField",objid);
}

var isOpenBrowser = false;
var isUserBrowserOk = false;
function openBrowser(fieldId,fieldSpanId,type,browserId,browserName,browserText,browserAction) {
	$("#browserFrames").removeClass("controlHide");
	$("#browserFrames iframe").hide();
	$("#add-plate-panel").hide();
	$("#add-con-panel").hide();
	$("#label-panel").hide();
	$("#detail-panel").hide();
	$("#searchDiv").hide();
	if (type == "1" || type == "2") {
		$("#hrmBrowserFrame").show();
		openHrmBrowser(window,fieldId,fieldSpanId,type);
    }else if(type == "3" || type == "4"){
    	$("#commonBrowserFrame").show();
    	if(type == "4"){
    		openCommonBrowser(window,fieldId,fieldSpanId,1,browserId,browserName,browserText,browserAction);
    	}else{
    		openCommonBrowser(window,fieldId,fieldSpanId,2,browserId,browserName,browserText,browserAction);
    	}
    }
	pushHistoryState({url:"f"});
	isOpenBrowser = true;
	isUserBrowserOk = false;
};

function openCommonBrowser(theWindow, fieldId, fieldSpanId, isMuti, browserId, browserName, browserText, browserAction){
	_BrowserWindow = theWindow;
	var defaultParamCfg = {
		"fieldId" : fieldId || "",			//字段id
		"fieldSpanId" : fieldSpanId || "",	//字段显示区域id
		"browserType" : isMuti || "1",		//类型：1.多选  2.单选
		"browserId" :  browserId || "",		//浏览框类型
		"browserName" : browserName || "",	//浏览框名称
		"browserText" : browserText || "",
		"browserAction" : browserAction || ""
	};
	try{
		var theDocument = theWindow.document;
		var fieldEle = theDocument.getElementById(fieldId);
		if(fieldEle){
			defaultParamCfg["selectedIds"] = fieldEle.value;
		}
	}catch(ex){}
	
	var commonBrowserFrame = document.getElementById("commonBrowserFrame");
	var commonBrowserWindow = commonBrowserFrame.contentWindow;
	if(commonBrowserWindow){
		commonBrowserWindow.resetBrowser(defaultParamCfg);
	}
}

var _BrowserWindow;
function openHrmBrowser(theWindow, fieldId, fieldSpanId, browserType){
	var paramstr = "fieldId="+(fieldId || "")+"&fieldSpanId="+(fieldSpanId || "")+"&browserType="+browserType;
	openHrmBrowser2(theWindow, paramstr);
}

function openHrmBrowser2(theWindow, paramstr){
	_BrowserWindow = theWindow;
	var defaultParamCfg = {
		"fieldId" : "",			//字段id
		"fieldSpanId" : "",		//字段显示区域id
		"browserType" : "1",	//类型：1.多选  2.单选
		"selectedIds" : ""		//选中的id，逗号分隔，如：1,2,3
	};
	
	var containSelectedId = false;
	var paramArr = paramstr.split("&");
	for(var i = 0; i < paramArr.length; i++){
		var oneParam = paramArr[i];
		var oneParamArr = oneParam.split("=");
		var	p_name = oneParamArr[0];
		var p_value = oneParamArr[1];
		defaultParamCfg[p_name] = p_value;
		if(p_name == "selectedIds"){
			containSelectedId = true;
		}
	}
	
	if(!containSelectedId){
		var fieldId = defaultParamCfg["fieldId"];
		var theDocument = theWindow.document;
		var fieldEle = theDocument.getElementById(fieldId);
		if(fieldEle){
			defaultParamCfg["selectedIds"] = fieldEle.value;
		}
	}
	var hrmBrowserFrame = document.getElementById("hrmBrowserFrame");
	var hrmBrowserWindow = hrmBrowserFrame.contentWindow;
	
	if(hrmBrowserWindow){
		hrmBrowserWindow.resetBrowser(defaultParamCfg);
	}
}

function setBrowserValue(type,value,showname){
	var browserType = $("#browserType");
	var currentField = browserType.attr("currentField");
	$("#"+currentField).val("");
	$("#"+currentField+"dpid").val("");
	$("#"+currentField+"all").val("");
	if(type==1){
		$("#"+currentField).val(value);
	}else if(type==2){
		$("#"+currentField+"dpid").val(value);
	}else if(type==3){
		$("#"+currentField+"all").val(1);
	}
	$("#"+currentField+"Span").html(showname);
}

function onBrowserBack(){
	$("#browserFrames").addClass("controlHide");
}

function onBrowserOk(result){
	var fieldId = result["fieldId"];
	var fieldSpanId = result["fieldSpanId"];
	var idValue = result["idValue"];
	var nameValue = result["nameValue"];
	$("#"+fieldId).val(idValue);
	$("#"+fieldSpanId).html(nameValue);
	isUserBrowserOk = true;
	doHistoryBack();
}